package org.zpmis.api.http;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zpmis.api.business.SysPermissionWebService;
import org.zpmis.common.base.BaseController;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.domain.role.qo.AddSysPermissionQo;
import org.zpmis.domain.role.qo.DelSysPermissionQo;
import org.zpmis.domain.role.qo.EditSysPermissionQo;
import org.zpmis.domain.role.qo.SysPermissionListQo;

import javax.annotation.Resource;

/**
* created with CodeGenerator
* description: 权限控制类
* @author  Yang
* date: 2021-5-31
* time: 14:00:27
*/
@RestController
@RequestMapping("/permission")
@Slf4j
public class SysPermissionController extends BaseController{

    @Resource
    private SysPermissionWebService sysPermissionWebService;


    /**
     * 新增权限信息
     * @param addSysPermissionQo
     * @return
     */
    @PostMapping("/addSysPermission")
    public PlatformResult addSysPermissionQo(@Validated @RequestBody AddSysPermissionQo addSysPermissionQo){
        try {
            sysPermissionWebService.addSysPermission(addSysPermissionQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysPermissionController]---addSysPermission--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysPermissionController]---addSysPermission--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 修改权限信息
     * @param editSysPermissionQo
     * @return
     */
    @PostMapping("/editSysPermission")
    public PlatformResult editSysPermission(@Validated @RequestBody EditSysPermissionQo editSysPermissionQo){
        try {
            sysPermissionWebService.editSysPermission(editSysPermissionQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysPermissionController]---editSysPermission--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysPermissionController]---editSysPermission--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 删除权限信息
     * @param delSysPermissionQo
     * @return
     */
    @PostMapping("/delSysPermission")
    public PlatformResult delSysPermission(@Validated @RequestBody DelSysPermissionQo delSysPermissionQo){
        try {
            sysPermissionWebService.delSysPermission(delSysPermissionQo);
            return PlatformResult.success();
        } catch (BusinessException e) {
            log.error("[SysPermissionController]---delSysPermission--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysPermissionController]---delSysPermission--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 权限列表查询
     * @param sysPermissionListQo
     * @return
     */
    @PostMapping("/sysPermissionList")
    public PlatformResult sysPermissionList(@RequestBody SysPermissionListQo sysPermissionListQo){
        try {
            return PlatformResult.success(sysPermissionWebService.sysPermissionList(sysPermissionListQo));
        } catch (BusinessException e) {
            log.error("[SysPermissionController]---sysPermissionList--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysPermissionController]---sysPermissionList--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 获取所有权限
     * @param
     * @return
     */
    @PostMapping("/allSysPermission")
    public PlatformResult allSysPermission(){
        try {
            return PlatformResult.success(sysPermissionWebService.allSysPermission());
        } catch (BusinessException e) {
            log.error("[SysPermissionController]---allSysPermission--->BusinessException:",e);
            return PlatformResult.failure(e.getResultCode());
        } catch (Exception e) {
            log.error("[SysPermissionController]---allSysPermission--->Exception:",e);
            return PlatformResult.failure();
        }
    }
}
